package com.guw.Snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;


//直接实现键盘监听的接口,以及定时器的接口
public class GamePanel extends JPanel implements KeyListener, ActionListener {
    //定义蛇的数据结构
    int length;
    int[] snakeX = new int[600];//蛇的x坐标25*25
    int[] snakeY = new int[500];//蛇的y坐标25*25
    String fx;//初始方向

    //事物的坐标
    int foodX;
    int foodY;
    Random random = new Random();

    //游戏当前状态:开始,停止
    boolean isStart = false;

    //定时器
    Timer timer = new Timer(200,this);//100毫秒执行一次

    //构造方法放入初始化方法
    public GamePanel() {
        init();
        //获得键盘和焦点事件
        this.setFocusable(true);//获得键盘焦点
        this.addKeyListener(this);//获得键盘监听事件
        timer.start();//游戏开始,定时器启动
    }

    //初始化方法
    public void init() {
        //初始长度3节
        length = 3;
        //初始脑袋坐标
        snakeX[0] = 100;
        snakeY[0] = 100;
        //第一个身体坐标
        snakeX[1] = 75;
        snakeY[1] = 100;
        //第二个身体坐标
        snakeX[2] = 50;
        snakeY[2] = 100;
        //初始头朝向
        fx = "R";

        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);
    }


    //绘制面板
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        //绘制静态面板
        this.setBackground(Color.WHITE);
        //头部广告栏
        Data.header.paintIcon(this, g, 25, 11);
        //游戏界面
        g.fillRect(25, 75, 850, 600);

        //把小蛇画上去
        if (fx.equals("R")) {
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇头初始化时向右边
        } else if (fx.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (fx.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if (fx.equals("L")) {
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
        }


        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);//根据身体坐标显示身体标签
        }

        Data.food.paintIcon(this,g,foodX,foodY);

        //游戏状态
        if(isStart==false){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));//设置字体
            g.drawString("按下空格开始游戏",300,300);
        }
    }


    //键盘监听
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //如果监听到空格键,开始状态取反
        if(keyCode == KeyEvent.VK_SPACE){
            isStart = !isStart;
            repaint();
        }
        //小蛇移动
        if(keyCode == KeyEvent.VK_UP){
            fx = "U";
        }else if(keyCode == KeyEvent.VK_DOWN){
            fx = "D";
        }else if(keyCode == KeyEvent.VK_LEFT){
            fx = "L";
        }else if(keyCode == KeyEvent.VK_RIGHT){
            fx = "R";
        }


    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }

    //事件监听,需要通过固定事件刷新
    @Override
    public void actionPerformed(ActionEvent e) {
        //如果游戏是开始状态,让小蛇动起来
        if(isStart){
            //吃食物
            if(snakeX[0] == foodX && snakeY[0] == foodY){
                //小蛇吃完长身体
                length++;
                //再次初始化食物
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 75 + 25 * random.nextInt(24);
            }

            //移动
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i-1];//向前移动一节
                snakeY[i] = snakeY[i-1];
            }

            //走向
            if(fx.equals("R")){
                snakeX[0] = snakeX[0]+25;
                //边界判断
                if(snakeX[0]>850){
                    snakeX[0]=25;
                }
            }else if(fx.equals("L")){
                snakeX[0] = snakeX[0]-25;
                //边界判断
                if(snakeX[0]<25){
                    snakeX[0]=850;
                }
            }else if(fx.equals("U")){
                snakeY[0] = snakeY[0]-25;
                //边界判断
                if(snakeY[0]<75){
                    snakeY[0]=650;
                }
            }else if(fx.equals("D")) {
                snakeY[0] = snakeY[0] + 25;
                //边界判断
                if (snakeY[0] > 650) {
                    snakeY[0] = 75;
                }
            }
            repaint();
        }
        timer.start();
    }
}


